import fs from 'fs'
import path from 'path'
import svelte from 'rollup-plugin-svelte';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import less from 'rollup-plugin-less'
import addAsset from './rollup-plugin-add-asset';
import markdown from './rollup-plugin-import-markdown-string';

const production = !process.env.ROLLUP_WATCH;

export default {
    input: 'http/src/app.js',
    output: {
        sourcemap: true,
        format: 'iife',
        name: 'app',
        file: 'public/bundle.index.js',
        globals: {
            'element-resize-detector': 'elementResizeDetectorMaker',
            'js-beautify': 'js_beautify',
            'dayjs': 'dayjs',
            'vkbeautify': 'vkbeautify',
            'tiny-pinyin': 'TinyPinyin',
            'js-base64': 'Base64',
            'serialize-javascript': 'serializeJavascript',
            'echarts': 'echarts',
            '@yuanliwei/web-loader': 'Loader',
            'crypto-js': 'CryptoJS',
            'split.js': 'Split',
            'jszip': 'jszip',
        }
    },
    external: [
        '@material/mwc-button',
        '@material/mwc-checkbox',
        '@material/mwc-circular-progress-four-color',
        '@material/mwc-drawer',
        '@material/mwc-dialog',
        '@material/mwc-formfield',
        '@material/mwc-icon',
        '@material/mwc-icon-button',
        '@material/mwc-fab',
        '@material/mwc-list',
        '@material/mwc-list/mwc-check-list-item',
        '@material/mwc-list/mwc-list-item',
        '@material/mwc-menu',
        '@material/mwc-radio',
        '@material/mwc-select',
        '@material/mwc-snackbar',
        '@material/mwc-textarea',
        '@material/mwc-textfield',
        '@material/mwc-top-app-bar',
        'element-resize-detector',
        'js-beautify',
        'dayjs',
        'vkbeautify',
        'tiny-pinyin',
        'js-base64',
        'serialize-javascript',
        '@yuanliwei/web-loader',
        'echarts',
        'crypto-js',
        'split.js',
        'jszip',
    ],
    plugins: [
        svelte({
            compilerOptions: {
                dev: !production
            }
        }),
        markdown(),
        resolve({
            browser: true,
            dedupe: ['svelte']
        }),
        less({
            insert: true,
            exclude: [],
            output: (css, id) => {
                return css.replace(/'([^']+.png)'/g, (a, b) => {
                    let img = path.join(path.dirname(id), b)
                    if (!fs.existsSync(img)) return a
                    return a.replace(b, 'data:image/*;base64,' + fs.readFileSync(img).toString('base64'))
                })
            }
        }),
        commonjs(),
        addAsset({
            name: 'index.html',
            buildContent: () => {
                let content = fs.readFileSync(path.join(__dirname, 'src', 'index.html'), 'utf-8')
                content = content.replace('<script src="app.js"></script>', '<script src="bundle.index.js"></script>')
                return content
            }
        }),
        !production && livereload('public/bundle.index.js'),
        production && terser(),
    ],
    watch: {
        clearScreen: false
    }
};
